// 贪心算法  摆动序列
var wiggleMaxLength = function(nums) {
    if(nums.length<=1) return nums.length;
    let result = 1;
    let pre = 0;
    let cur = 0;
    for(let i = 1; i < nums.length; i++) {
        cur = nums[i]-nums[i-1];
        if(cur > 0 && pre <= 0 || cur < 0 && pre >=0) {
            ++result;
            pre = cur;
        }
    }
    return result;
};
var wiggleMaxLength = function(nums) {
    const n = nums.length;
    if (n < 2) { 
        return n;
    }

    let up = down = 1;
    for (let i = 1; i < n; i++) {
        if (nums[i] > nums[i - 1]) {
            up = down + 1;
        } else if (nums[i] < nums[i - 1]) {
            down = up + 1;
        }
    }
    return Math.max(up, down);
};